package com.bestcem.xm.ticket.grpc.client.convert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.DeliverAgainRecordDTO;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.DeliverAgainTaskDTO;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.SMSBalanceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * TicketBaseDeliverGrpcService 中 response 转 DTO
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/30 14:28
 */
@Slf4j
public class TicketBaseDeliverConvert {
    private TicketBaseDeliverConvert() {
    }

    /**
     * AgainDeliverRecordResponse to DeliverAgainRecordDTO
     *
     * @param response 获取单个AgainDeliverRecord的response
     * @return 二次投放记录
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:09 2021/8/30
     */
    public static DeliverAgainRecordDTO responseToDeliverAgainRecordDTO(AgainDeliverRecordResponse response) {
        AgainDeliverRecord againDeliverRecord = response.getAgainDeliverRecord();
        return infoToDeliverAgainRecordDTO(againDeliverRecord);
    }

    /**
     * AgainDeliverRecord to DeliverAgainRecordDTO
     *
     * @param againDeliverRecord proto中定义的againDeliverRecord
     * @return 二次投放记录
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:16 2021/8/30
     */
    private static DeliverAgainRecordDTO infoToDeliverAgainRecordDTO(AgainDeliverRecord againDeliverRecord) {
        if (Objects.isNull(againDeliverRecord)) {
            return null;
        }
        DeliverAgainRecordDTO deliverAgainRecordDTO = new DeliverAgainRecordDTO();
        deliverAgainRecordDTO.setId(againDeliverRecord.getId());
        deliverAgainRecordDTO.setName(againDeliverRecord.getName());
        deliverAgainRecordDTO.setContactNo(againDeliverRecord.getContactNo());
        deliverAgainRecordDTO.setSendStatus(againDeliverRecord.getSendStatusValue());
        deliverAgainRecordDTO.setSurveyStatus(againDeliverRecord.getSurveyStatusValue());
        deliverAgainRecordDTO.setLink(againDeliverRecord.getLink());
        deliverAgainRecordDTO.setContents(againDeliverRecord.getContents());
        deliverAgainRecordDTO.setTicketId(againDeliverRecord.getTicketId());
        deliverAgainRecordDTO.setAgainDeliverTaskId(againDeliverRecord.getAgainDeliverTaskId());
        deliverAgainRecordDTO.setDeliverType(againDeliverRecord.getDeliverTypeValue());
        deliverAgainRecordDTO.setUrgeNums(againDeliverRecord.getUrgeNums());
        return deliverAgainRecordDTO;
    }

    /**
     * AgainDeliverTaskSearchResponse to List<DeliverAgainTaskDTO>
     *
     * @param response 通过查询条件批量一次性获取所有符合条件的AgainDeliverTask的response
     * @return 二次投放任务
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:42 2021/8/30
     */
    public static List<DeliverAgainTaskDTO> responseToDeliverAgainTaskDTOList(AgainDeliverTaskSearchResponse response) {
        List<AgainDeliverTask> againDeliverTaskList = response.getAgainDeliverTaskListList();
        List<DeliverAgainTaskDTO> deliverAgainTaskDTOList = new ArrayList();
        if (!CollectionUtils.isEmpty(againDeliverTaskList)) {
            againDeliverTaskList.forEach(againDeliverTask -> {
                if (Objects.nonNull(againDeliverTask)) {
                    DeliverAgainTaskDTO deliverAgainTaskDTO = infoToDeliverAgainTaskDTO(againDeliverTask);
                    deliverAgainTaskDTOList.add(deliverAgainTaskDTO);
                }
            });
        }
        return deliverAgainTaskDTOList;
    }

    /**
     * AgainDeliverTask to DeliverAgainTaskDTO
     *
     * @param againDeliverTask proto中定义的AgainDeliverTask
     * @return 二次投放任务
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:44 2021/8/30
     */
    private static DeliverAgainTaskDTO infoToDeliverAgainTaskDTO(AgainDeliverTask againDeliverTask) {
        if (Objects.isNull(againDeliverTask)) {
            return null;
        }
        DeliverAgainTaskDTO deliverAgainTaskDTO = new DeliverAgainTaskDTO();
        deliverAgainTaskDTO.setId(againDeliverTask.getId());
        deliverAgainTaskDTO.setName(againDeliverTask.getName());
        deliverAgainTaskDTO.setRawProjectId(againDeliverTask.getRawProjectId());
        deliverAgainTaskDTO.setCreator(againDeliverTask.getCreator());
        String conditions = againDeliverTask.getConditions();
        List<JSONObject> jsonObjectList = null;
        if (Objects.nonNull(conditions)) {
            boolean validate = JSONValidator.from(conditions).validate();
            if (validate) {
                try {
                    jsonObjectList = JSONArray.parseArray(conditions, JSONObject.class);
                } catch (JSONException e) {
                    log.error("JSON字符串转换为数组异常, jsonStr={}", conditions);
                }
            }
        }
        deliverAgainTaskDTO.setConditions(jsonObjectList);
        deliverAgainTaskDTO.setTtype(againDeliverTask.getTtypeValue());
        deliverAgainTaskDTO.setCheckSubmit(String.valueOf(againDeliverTask.getChecksubmit()));
        deliverAgainTaskDTO.setConditionRelation(againDeliverTask.getConditionRelationValue());
        deliverAgainTaskDTO.setOrgID(againDeliverTask.getOrgId());
        deliverAgainTaskDTO.setDeliverProjectId(againDeliverTask.getDeliverProjectId());
        deliverAgainTaskDTO.setSmsTemplateId(againDeliverTask.getSmsTemplateId());
        deliverAgainTaskDTO.setSmsTemplateContent(againDeliverTask.getSmsTemplateContent());
        deliverAgainTaskDTO.setSmsTemplateTitle(againDeliverTask.getSmsTemplateTitle());
        return deliverAgainTaskDTO;


    }

    /**
     * balance to SMSBalanceDTO
     *
     * @param balance proto中定义的SMSBalance
     * @return 短信余额DTO
     * @author <yuming.pan@idiaoyan.com>
     * @date 13:56 2021/8/31
     */
    public static SMSBalanceDTO infoToSMSBalanceDTO(SMSBalance balance) {
        if (Objects.isNull(balance)) {
            return null;
        }
        SMSBalanceDTO sMSBalanceDTO = new SMSBalanceDTO();
        sMSBalanceDTO.setId(balance.getId());
        sMSBalanceDTO.setOrgId(balance.getOrgId());
        sMSBalanceDTO.setCode(balance.getCode());
        sMSBalanceDTO.setBalance(balance.getBalance());
        sMSBalanceDTO.setSuccessNumber(balance.getSuccessNumber());
        sMSBalanceDTO.setSignature(balance.getSignature());
        sMSBalanceDTO.setVersion(balance.getVersion());
        return sMSBalanceDTO;
    }
}
    